package com.manlitech.cloudboot.shardingjdbc.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manlitech.cloudboot.common.utils.ResultUtil;
import com.manlitech.cloudboot.shardingjdbc.mapper.UserInfoMapper;
import com.manlitech.cloudboot.shardingjdbc.pojo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shensg
 * @date 2021/7/21 13:36
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
@DS("gits_sharding")
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> {

    @Autowired
    UserInfoMapper userInfoMapper;

    @DS("gits_sharding")
    public Object findUserSuccess() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        Page<UserInfo> page = new Page<>(1,1);
        IPage<UserInfo> iPage = userInfoMapper.selectPage(page, queryWrapper);
        log.info("总页数={}", iPage.getPages());
        log.info("总记录数={}", iPage.getTotal());
        List<UserInfo> users = iPage.getRecords();
        for (UserInfo user : users) {
            log.info("查询内容={}", JSON.toJSONString(user));
        }
        return iPage;
    }

    public Object findByName() {
        return ResultUtil.success(this.lambdaQuery().eq(UserInfo::getName , "abc").list());
    }
}
